﻿using System;
using System.Collections.Generic;
using System.Text;
using Mighty.Common;
using System.Collections;

namespace Mighty.Brain
{
    public class KiKiBrain : BasicBrain
    {
        /// <summary>
        /// 자신의 베팅차례에서 제일 많은 무늬를 기루다로 선택한다 그리고 Ace, K, Q, J 의 존재여부에 따라 배팅을 한다
        /// </summary>

        public override Betting BettingTicEvent()
        {
            Betting betting = new Betting();
            betting.isPass = true;

            // 각 무늬의 개수를 센다
            int heart = 0;
            int clover = 0;
            int spade = 0;
            int diamond = 0;
            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape == Shape.Clover) clover++;
                else if (card.shape == Shape.Heart) heart++;
                else if (card.shape == Shape.Spade) spade++;
                else if (card.shape == Shape.Diamond) diamond++;
            }

            // 가장 많은 무늬를 찾아 낸다
            Giruda selectedShape = Giruda.Heart;
            int selectedCount = heart;

            if (clover > selectedCount)
            {
                selectedShape = Giruda.Clover;
                selectedCount = clover;
            }
            if (spade > selectedCount)
            {
                selectedShape = Giruda.Spade;
                selectedCount = spade;
            }
            if (diamond > selectedCount)
            {
                selectedShape = Giruda.Diamond;
                selectedCount = diamond;
            }
            betting.giruda = selectedShape;

            // 정해진 기루다모양에서 Ace, K, Q, J 가 있는지 찾는다
            bool have_girudaAce = false;
            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape != (Shape)selectedShape) continue;
                if (card.number == 14) have_girudaAce = true;
            }

            bool have_girudaK = false;
            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape != (Shape)selectedShape) continue;
                if (card.number == 13) have_girudaK = true;
            }

            bool have_girudaQ = false;
            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape != (Shape)selectedShape) continue;
                if (card.number == 12) have_girudaQ = true;
            }

            bool have_girudaJ = false;
            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape != (Shape)selectedShape) continue;
                if (card.number == 11) have_girudaJ = true;
            }

            // Ace, K, Q, J 존재여부에 따라 max_betting을 정한다
            int max_betting = 0;
            if (selectedCount > 3)
            {
                if (have_girudaAce)
                {
                    if (have_girudaK)
                    {
                        if (have_girudaQ || have_girudaJ)
                        {
                            if (selectedCount > 5) max_betting = 16;
                            else max_betting = 15;

                        }

                        else
                        {
                            if (selectedCount > 5) max_betting = 15;
                            else if (selectedCount > 4) max_betting = 14;
                            else max_betting = 13;
                        }

                    }

                    else
                    {
                        if (selectedCount > 5) max_betting = 13;

                    }
                }

                else
                {
                    if (have_girudaK)
                    {
                        if (have_girudaQ || have_girudaJ)
                        {
                            if (selectedCount > 5) max_betting = 15;
                            else max_betting = 14;

                        }

                        else
                        {
                            if (selectedCount > 5) max_betting = 14;
                            else if (selectedCount > 4) max_betting = 13;

                        }

                    }

                }

            }

            //배팅을 한다
            if (max_betting != 0)
            {
                if (gameInfo.betting.limit == 0)
                {
                    betting.isPass = false;
                    betting.limit = 13;
                }

                if (gameInfo.betting.limit > 0)
                {
                    if (gameInfo.betting.limit < max_betting)
                    {
                        betting.isPass = false;
                        betting.limit = gameInfo.betting.limit + 1;
                    }
                }



            }

            else
            {
                betting.isPass = true;
            }

            return betting;


        }

        /// <summary>
        /// [기본행동] 마이티프랜드 > 조커프랜드 > 기루다Ace프랜드 > 초구프랜드
        /// </summery>

        public override FriendPlayer ChoiceFriendEvent()
        {
            bool haveMighty = false;
            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape == gameInfo.mighty.shape && card.number == 14) haveMighty = true;

            }

            bool haveJoker = false;
            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape == Shape.Joker) haveJoker = true;

            }

            bool haveGiruda_ace = false;
            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape == (Shape)gameInfo.betting.giruda && card.number == 14) haveGiruda_ace = true;

            }

            if (haveMighty)
            {
                if (haveJoker)
                {
                    if (haveGiruda_ace)
                    {
                        gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.firstwin;
                    }
                    else
                    {
                        gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                        gameInfo.friend.cardCondition = new Card((Shape)gameInfo.betting.giruda, 14);
                    }

                }
                else
                {
                    gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                    gameInfo.friend.cardCondition = new Card(Shape.Joker, 1);
                }
            }
            else
            {
                gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                gameInfo.friend.cardCondition = new Card(gameInfo.mighty.shape, 14);
            }

            return gameInfo.friend;
        }

        /// <summary>
        /// 카드낼차례
        /// </summary>


        public override HandedCard RoundTicEvent()
        {

            HandedCard[] h = new HandedCard[10];

            if ((gameInfo.attackPlayer.name == gameInfo.myInfo.name && gameInfo.attackPlayer.position == gameInfo.myInfo.position) || (gameInfo.friend.name == gameInfo.myInfo.name && gameInfo.friend.position == gameInfo.myInfo.position))
            {

                int n = 0;
                int minValue = 0;
                int minPosition = 0;
                for (int i = 0; i < 10; i++)
                {
                    if (gameInfo.myCards[i].isPossible)
                    {
                        h[n++] = gameInfo.myCards[i];
                        if (minValue > gameInfo.myCards[i].number)
                        {
                            minValue = gameInfo.myCards[i].number;
                            minPosition = i;
                        }
                    }
                }

                if (gameInfo.roundIdx == 0)
                {
                    for (int i = 0; i < n; i++)
                    {
                        if ((h[i].number == 14))
                        {
                            return h[i];
                        }
                    }
                    return gameInfo.myCards[minPosition];
                }
                else
                {
                    return gameInfo.myCards[minPosition];
                } 
            }
            else
            {
                int n = 0;
                int minValue = 0;
                int minPosition = 0;
                for (int i = 0; i < 10; i++)
                {
                    if (gameInfo.myCards[i].isPossible)
                    {
                        h[n++] = gameInfo.myCards[i];
                        if (minValue > gameInfo.myCards[i].number)
                        {
                            minValue = gameInfo.myCards[i].number;
                            minPosition = i;
                        }
                    }
                }

                return gameInfo.myCards[minPosition];
            }

        }
    }
}     

 